/**
 * output package name
 */
package com.kingdee.eas.custom.hr.attendance.client;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.swing.KDCheckBox;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.eas.base.commonquery.client.CustomerParams;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.OrgUnitInfo;
import com.kingdee.eas.custom.hr.attendance.AttendanceTimeConfigFactory;
import com.kingdee.eas.custom.hr.attendance.AttendanceTimeConfigInfo;
import com.kingdee.eas.hr.time.client.TimeAttendanceUtils;
import com.kingdee.eas.rptclient.newrpt.util.MsgBox;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.util.LocaleUtils;

/**
 * output class name
 */
public class AttendanceCommQueryPanelUI extends
		AbstractAttendanceCommQueryPanelUI {
	private static final Logger logger = CoreUIObject
			.getLogger(AttendanceCommQueryPanelUI.class);

	/**
	 * output class constructor
	 */
	public AttendanceCommQueryPanelUI() throws Exception {
		super();
	}

	public void onLoad() throws Exception {
		super.onLoad();
		TimeAttendanceUtils.initF7Selector(this, this.prmtAdminOrgUnit);
	}

	@Override
	public CustomerParams getCustomerParams() {
		CustomerParams cp = new CustomerParams();
		if (this.prmtAdminOrgUnit.getData() != null) {
			AdminOrgUnitInfo orgUnit = (AdminOrgUnitInfo) this.prmtAdminOrgUnit
					.getData();
			cp.addCustomerParam("OrgUnit", orgUnit.getId().toString());
			cp.addCustomerParam("OrgUnit_Name_L1", orgUnit.getName(LocaleUtils
					.getLocale("L1")));
			cp.addCustomerParam("OrgUnit_Name_L2", orgUnit.getName(LocaleUtils
					.getLocale("L2")));
			cp.addCustomerParam("OrgUnit_Name_L3", orgUnit.getName(LocaleUtils
					.getLocale("L3")));
		}

		if (this.prmtAttendancePeriod.getValue() != null) {
			AttendanceTimeConfigInfo configInfo = (AttendanceTimeConfigInfo) this.prmtAttendancePeriod
					.getValue();
			cp.putDate("timeFrom", configInfo.getBeginDate());
			cp.putDate("timeTo", configInfo.getEndDate());
			cp.addCustomerParam("attendanceTimeConfig", configInfo.getId()
					.toString());
		}

		cp.putBoolean("chkInclude", this.isInclude.isSelected());
		return cp;

	}

	@Override
	public void setCustomerParams(CustomerParams cp) {
		clear();
		if (cp == null)
			return;
		try {
			String orgID = cp.getCustomerParam("OrgUnit");
			if (orgID != null) {
				AdminOrgUnitInfo orgUnit = AdminOrgUnitFactory
						.getRemoteInstance().getAdminOrgUnitInfo(
								new ObjectUuidPK(orgID));
				this.prmtAdminOrgUnit.setValue(orgUnit);
			}

			String attendanceTimeConfigId = cp
					.getCustomerParam("attendanceTimeConfig");
			if (attendanceTimeConfigId != null) {
				AttendanceTimeConfigInfo attendanceTimeConfigInfo = AttendanceTimeConfigFactory
						.getRemoteInstance().getAttendanceTimeConfigInfo(
								new ObjectUuidPK(attendanceTimeConfigId));
				this.prmtAttendancePeriod.setValue(attendanceTimeConfigInfo);
			}

			this.isInclude.setSelected(cp.getBoolean("chkInclude"));
		} catch (Exception e) {
			super.handUIException(e);
		}
	}

	public void clear() {
		super.clear();
		this.prmtAdminOrgUnit.setValue(null);
		this.prmtAttendancePeriod.setValue(null);
		this.isInclude.setSelected(false);
	}

	public boolean verify() {
		if (this.prmtAttendancePeriod.getValue() == null) {
			MsgBox.showInfo(this, EASResource
					.getString(TimeAttendanceUtils.strResource
							+ "STARTDATE_MUST_ENDDATE"));
			return false;
		} else {
			return true;
		}
	}

	/**
	 * output storeFields method
	 */
	public void storeFields() {
		super.storeFields();
	}

	public FilterInfo getFilterInfo() {
		OrgUnitInfo orgInfo = (OrgUnitInfo) this.prmtAdminOrgUnit.getData();
		FilterInfo filterInfo = new FilterInfo();
		if (orgInfo == null) {
			filterInfo.getFilterItems().add(new FilterItemInfo("id", null));
			return filterInfo;
		}
		if (orgInfo != null) {
			if (this.isInclude.isSelected()) {
				FilterInfo orgFi = new FilterInfo();
				orgFi.getFilterItems().add(
						new FilterItemInfo("dept.longNumber", orgInfo
								.getLongNumber()));
				orgFi.getFilterItems().add(
						new FilterItemInfo("dept.longNumber", orgInfo
								.getLongNumber()
								+ "!%", CompareType.LIKE));

				orgFi.setMaskString("(#0 or #1)");
				try {
					filterInfo.mergeFilter(orgFi, "and");
				} catch (BOSException e) {
					super.handUIException(e);
				}
			} else {
				filterInfo.getFilterItems().add(
						new FilterItemInfo("dept.id", orgInfo.getId()
								.toString()));
			}
		}
		return filterInfo;
	}

	public AdminOrgUnitInfo getAdminOrgUnit() {
		return (AdminOrgUnitInfo) this.prmtAdminOrgUnit.getValue();
	}

	public AttendanceTimeConfigInfo getAttendnacePeriod() {
		return (AttendanceTimeConfigInfo) this.prmtAttendancePeriod.getValue();
	}

	public boolean isInclude() {
		return this.isInclude.isSelected();
	}
}